package com.cqemme.activitiservice.controller;


import com.cqemme.activitiservice.config.ActivitiProcessProperties;
import com.cqemme.activitiservice.service.AlarmService;
import com.cqemme.activitiservice.service.MyHistoryService;
import com.cqemme.comm.constraint.DefaultValues;
import com.cqemme.comm.enums.AlarmType;
import com.cqemme.comm.po.CurrentLoginUser;
import com.cqemme.comm.po.Result;
import com.cqemme.comm.utils.JackSonUtils;
import com.cqemme.comm.utils.StringUtils;
import org.activiti.engine.HistoryService;
import org.activiti.engine.RuntimeService;
import org.activiti.engine.history.HistoricActivityInstance;
import org.activiti.engine.history.HistoricProcessInstance;
import org.activiti.engine.runtime.ProcessInstance;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * 测试流程
 * @author nejery
 *
 */
@RestController
@RequestMapping(value="/test")
public class TestController {

    @Resource
    private AlarmService alarmService;

    @Resource
    private RuntimeService runtimeService;

    @Resource
    private MyHistoryService myHistoryService;

    @Resource
    private HistoryService historyService;

    @Resource
    private ActivitiProcessProperties activitiProcessProperties;

    @PostMapping("/startActiviti")
    public Result startActiviti(@RequestBody Map<String,Object> map){
        Result result = new Result();
        alarmService.handleAlarm(JackSonUtils.beanToString(map));
        return result;
    }


    @GetMapping("/testProperties")
    public Result testProperties(){
        Result result = new Result();
        //result.setData(activitiProcessProperties.getUrls());
        return result;
    }


    @GetMapping("/getTask")
    public Result getTask(String id){
        Result result = new Result();
        List<HistoricActivityInstance> historicActivityInstances = myHistoryService.selectHistoryNode(id);
        result.setData(historicActivityInstances);
        return result;
    }


    @GetMapping("/testSingle")
    public Result testSingle(String id){
        Result result = new Result();
        runtimeService.startProcessInstanceByKey("test1");
        return result;
    }

    @GetMapping("/testSingle1")
    public Result testSingle1(String id){
        Result result = new Result();
        runtimeService.signalEventReceived("test");
        return result;
    }

   // @GetMapping("/deleteProcessInstance")
    public Result deleteProcessInstance(@RequestParam(value = "key", defaultValue = "", required = false) String key, CurrentLoginUser currentLoginUser){
        Result result = new Result();
        List<ProcessInstance> list;
        List<HistoricProcessInstance> list1;
        if(StringUtils.isEmpty(key)){
            list = runtimeService.createProcessInstanceQuery().list();
            list1 = historyService.createHistoricProcessInstanceQuery().list();


        }else{
            list = runtimeService.createProcessInstanceQuery()
                    .processDefinitionKey(key)
                    .processDefinitionCategory(DefaultValues.ACTIVITI_NAME_SPACE_PREFIX+currentLoginUser.getOrgId())
                    .list();
            list1 = historyService.createHistoricProcessInstanceQuery()
                    .processDefinitionKey(key)
                    .processDefinitionCategory(DefaultValues.ACTIVITI_NAME_SPACE_PREFIX+currentLoginUser.getOrgId())
                    .list();

        }

        for(ProcessInstance processInstance: list){
            runtimeService.deleteProcessInstance(processInstance.getProcessInstanceId(),"删除数据");
        }

        for(HistoricProcessInstance historicProcessInstance: list1) {
            historyService.deleteHistoricProcessInstance(historicProcessInstance.getSuperProcessInstanceId());
        }

        return result;
    }





}
